//
//  TNHRequestHelper.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/17/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "TNHRequestHelper.h"
#import "NSDictionary+Utils.h"

@implementation TNHRequestHelper

+ (void)sendGetRequest:(NSString *)requestPath withTag:(NSInteger)tag params:(NSDictionary *)params delegate:(id<ASIHTTPRequestDelegate>)delegate {
    NSMutableString *_escapes = [NSMutableString stringWithString:requestPath];
    if (params != nil && params.allKeys.count > 0) {
        [_escapes appendFormat:@"?%@", [params toQueryString]];
    }
    NSURL *_url = [NSURL URLWithString:[_escapes stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:_url];
    [_request setRequestMethod:@"GET"];
    _request.delegate = delegate;
    _request.tag = tag;
    [_request setTimeOutSeconds:60];    
    [_request startAsynchronous];
}

+ (void)sendPostRequest:(NSString *)requestPath withTag:(NSInteger)tag params:(NSDictionary *)params delegate:(id<ASIHTTPRequestDelegate>)delegate {
    NSString *_escapes = [requestPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *_url = [NSURL URLWithString:_escapes];
    ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:_url];
    [_request setRequestMethod:@"POST"];
    _request.delegate = delegate;
    _request.tag = tag;
    NSMutableDictionary *dict = [params toMutableDictionary];
    for (id key in dict) {
        id val = [dict objectForKey:key];
        if ([val isKindOfClass:[NSData class]]) {
            [_request addData:val withFileName:@"avatar.jpg" andContentType:@"image/jpeg" forKey:key];
        }
        else {
            [_request setPostValue:val forKey:key];
        }
    }
    [_request setTimeOutSeconds:60];
    [_request startAsynchronous];
}

+ (void)sendPutRequest:(NSString *)requestPath withTag:(NSInteger)tag params:(NSDictionary *)params delegate:(id<ASIHTTPRequestDelegate>)delegate {
    NSMutableString *_escapes = [NSMutableString stringWithString:requestPath];
    if (params != nil && params.allKeys.count > 0) {
        [_escapes appendFormat:@"?%@", [params toQueryString]];
    }
    

    NSURL *_url = [NSURL URLWithString:_escapes];
    ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:_url];
    [_request setRequestMethod:@"PUT"];
    _request.delegate = delegate;
    _request.tag = tag;
    NSMutableDictionary *dict = [params toMutableDictionary];
    for (id key in dict) {
        id val = [dict objectForKey:key];
        [_request setPostValue:val forKey:key];
    }
    [_request setTimeOutSeconds:60];    
    [_request startAsynchronous];
}

+ (void)sendDeleteRequest:(NSString *)requestPath withTag:(NSInteger)tag params:(NSDictionary *)params delegate:(id<ASIHTTPRequestDelegate>)delegate {
    NSMutableString *_escapes = [NSMutableString stringWithString:requestPath];
    if (params != nil && params.allKeys.count > 0) {
        [_escapes appendFormat:@"?%@", [params toQueryString]];
    }
    NSURL *_url = [NSURL URLWithString:_escapes];
    ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:_url];
    [_request setRequestMethod:@"DELETE"];
    _request.delegate = delegate;
    _request.tag = tag;
    [_request setTimeOutSeconds:60];    
    [_request startAsynchronous];
}

+ (void)downloadFrom:(NSString *)url saveTo:(NSString *)path withTag:(NSInteger)tag delegate:(id<ASIHTTPRequestDelegate>)delegate {
    NSString *_escapes = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *_url = [NSURL URLWithString:_escapes];
    ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:_url];
    _request.downloadDestinationPath = path;
    _request.delegate = delegate;
    _request.tag = tag;
    [_request setTimeOutSeconds:60];    
    [_request startAsynchronous];
}

+ (void)uploadFile:(NSString *)path to:(NSString *)url tag:(NSInteger)tag params:(NSDictionary *)params delegate:(id<ASIHTTPRequestDelegate>)delegate {
    NSString *_escapes = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *_url = [NSURL URLWithString:_escapes];
    ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:_url];
    _request.delegate = delegate;
    _request.tag = tag;
    NSString *fileName = nil;
    NSString *contentType = nil;
    NSString *key = nil;
    if (params != nil) {
        NSMutableDictionary *dict = [params toMutableDictionary];
        for (id key in dict) {
            id val = [dict objectForKey:key];
            [_request addPostValue:val forKey:key];
        }
        if ([[dict allKeys] containsObject:@"fileName"]) {
            fileName = [dict objectForKey:@"fileName"];
        }
        else {
            NSArray *pathComs = [path pathComponents];
            fileName = [pathComs objectAtIndex:pathComs.count - 1];
        }
        if ([[dict allKeys] containsObject:@"contentType"]) {
            contentType = [dict objectForKey:@"contentType"];
        }
        else {
            NSString *extension = [path pathExtension];
            if ([extension isEqualToString:@"png"]) {
                contentType = @"image/png";
            }
            else
                if ([extension isEqualToString:@"jpg"]) {
                    contentType = @"image/jpg";
                }
                else
                    if ([extension isEqualToString:@"caf"]) {
                        contentType = @"image/png";
                    }
                    else
                        contentType = @"text/plain";
        }
        if ([[dict allKeys] containsObject:@"key"]) {
            key = [dict objectForKey:@"key"];
        }
        else {
            key = @"key";
        }
    }
    NSData *data = [NSData dataWithContentsOfFile:path];
    [_request addData:data withFileName:fileName andContentType:contentType forKey:key];
    [_request setTimeOutSeconds:60];
    [_request startAsynchronous];
}

+ (id)objectWithString:(NSString *)theJson {
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    id object = [parser objectWithString:theJson];
    [parser release];
    return object;
}

@end
